package com.one.blocks.rbac.controller;

import com.one.blocks.common.response.ResponseHelper;
import com.one.blocks.common.response.WebResponse;
import com.one.blocks.common.util.TreeUtil;
import com.one.blocks.rbac.controller.vo.SysAccountVo;
import com.one.blocks.rbac.controller.vo.SysMenuTreeVo;
import com.one.blocks.rbac.convert.SysMenuConvert;
import com.one.blocks.rbac.convert.SysUserConvert;
import com.one.blocks.rbac.domain.SysMenu;
import com.one.blocks.rbac.domain.SysUser;
import com.one.blocks.rbac.enums.MenuTypeEnum;
import com.one.blocks.rbac.log.Log;
import com.one.blocks.rbac.manager.SysMenuManager;
import com.one.blocks.rbac.manager.SysUserManager;
import com.one.blocks.rbac.util.SubjectHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:idler41@163.con">linfuxin</a> created on 2023-11-05 16:19:14
 */
@Api(tags = "系统账号接口")
@RestController
@RequestMapping("/sys/account")
@Slf4j
@Validated
@Log(module = "账号")
public class SysAccountController {

    @Autowired
    private SysUserManager sysUserManager;

    @Autowired
    private SysMenuManager sysMenuManager;

    @ApiOperation("获取账号用户信息")
    @GetMapping(value = "/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<SysAccountVo> info() {
        return ResponseHelper.success(SysUserConvert.INSTANCE.entityToAccountVo(sysUserManager.get(SubjectHelper.getSubjectId())));
    }

    @ApiOperation("获取账号目录信息")
    @GetMapping(value = "/menu/tree", produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<List<SysMenuTreeVo>> myMenuTree() {
        List<SysMenu> sysMenuList = sysMenuManager.findTreeByUser(SubjectHelper.getSubjectId());
        if (CollectionUtils.isEmpty(sysMenuList)) {
            return ResponseHelper.success(null);
        }
        // 去除按钮相关
        sysMenuList = sysMenuList.stream().filter(i-> !MenuTypeEnum.BUTTON.match(i.getType())).collect(Collectors.toList());
        return ResponseHelper.success(TreeUtil.buildStrictTree(SysMenuConvert.INSTANCE.entityToTreeVoList(sysMenuList)));
    }

    @ApiOperation("获取账号权限信息")
    @GetMapping(value = "/permission", produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Set<String>> myPermission() {
        return ResponseHelper.success(sysMenuManager.findPermissionByUser(SubjectHelper.getSubjectId()));
    }

    @ApiOperation("获取账号所有信息")
    @GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<SysAccountVo> all() {
        SysUser sysUser = sysUserManager.get(SubjectHelper.getSubjectId());
        SysAccountVo data = new SysAccountVo();
        data.setNickName(sysUser.getNickname());
        data.setAvatar(sysUser.getAvatar());
        data.setPermissions(sysMenuManager.findPermissionByUser(SubjectHelper.getSubjectId()));

        List<SysMenu> sysMenuList = sysMenuManager.findTreeByUser(SubjectHelper.getSubjectId());
        if (CollectionUtils.isNotEmpty(sysMenuList)) {
            // 去除按钮相关
            sysMenuList = sysMenuList.stream().filter(i-> !MenuTypeEnum.BUTTON.match(i.getType())).collect(Collectors.toList());
            data.setMenus(TreeUtil.buildStrictTree(SysMenuConvert.INSTANCE.entityToTreeVoList(sysMenuList)));
        }
        return ResponseHelper.success(data);
    }
}
